package com.syz.web.service.Impl;

import com.syz.web.dao.ICourseDao;
import com.syz.web.dao.Impl.CourseDaoImpl;
import com.syz.web.pojo.Course;
import com.syz.web.pojo.Student;
import com.syz.web.service.ICourseService;
import com.syz.web.util.PageInfo;

import java.util.List;

public class CourseServiceImpl implements ICourseService {
    private ICourseDao courseDao = new CourseDaoImpl();

    //业务：为了封装PageInfo，查询两次数据库
    @Override
    public PageInfo<Course> selectByPage(Integer pageNo, Integer pageSize) {
        List<Course> list = courseDao.selectByPage(pageNo, pageSize);
        Integer totalCount = courseDao.selectTotalCount();
        int totalPage = (int)Math.ceil((double)totalCount / pageSize);
        PageInfo<Course> pageInfo = new PageInfo<>(list, totalPage, pageNo, pageSize);
        return pageInfo;
    }

    @Override
    public boolean deleteById(int id) {
        int count = courseDao.deleteById(id);
        return count == 1 ? true : false;
    }

    @Override
    public boolean add(String name, String credit) {
        int count=courseDao.add(name,credit);
        return count>0?true:false;
    }

    @Override
    public boolean update(String id, String name, String credit) {
        int count=courseDao.update(id,name,credit);
        return count>0?true:false;
    }

    @Override
    public Course getCourseUpdatePage(String id) {
        return courseDao.getCourseUpdatePage(id);
    }
}
